package cn.hy.batch.controller;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class StartJobController {
   @Resource
    private JobLauncher jobLauncher;
   @Resource
    private Job jobDemo2; //注入job

    @RequestMapping("/jobStart/{name}")
    public String jobStart(@PathVariable String name) throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
        //封装参数
        JobParameters jobParameter = new JobParametersBuilder().addString("name", name).toJobParameters();
        jobLauncher.run(jobDemo2, jobParameter);
        return "job start";
    }
}
